package indi.zhifa.study2025.oa.admin.facade.dep.impl;

import indi.zhifa.engine.core.common.utils.DtoEntityUtil;
import indi.zhifa.engine.mpenhance.utils.DbDtoEntityUtil;
import indi.zhifa.study2025.oa.admin.facade.dep.IPositionFacade;
import indi.zhifa.study2025.oa.dao.mybatis.dep.mapperOut.PositionTagMapperOut;
import indi.zhifa.study2025.oa.dao.po.GenPositionEntity;
import indi.zhifa.study2025.oa.dao.po.GenPositionTagEntity;
import indi.zhifa.study2025.oa.model.expose.dep.dto.PositionCreateDto;
import indi.zhifa.study2025.oa.model.expose.dep.dto.PositionEditDto;
import indi.zhifa.study2025.oa.model.expose.dep.vo.PositionTagMinVo;
import indi.zhifa.study2025.oa.model.expose.dep.vo.PositionVo;
import indi.zhifa.study2025.oa.service.dep.IPositionService;
import indi.zhifa.study2025.oa.service.dep.IPositionTagService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Component
public class PositionFacadeImpl implements IPositionFacade {

    final IPositionService mPositionService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PositionVo create(PositionCreateDto pPositionCreateDto) {
        GenPositionEntity newPositionEntity = DbDtoEntityUtil.createFromDto(pPositionCreateDto, GenPositionEntity.class);
        newPositionEntity = mPositionService.create(newPositionEntity,pPositionCreateDto.getTagIds());
        PositionVo rtn = DtoEntityUtil.trans(newPositionEntity, PositionVo.class);
        return rtn;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PositionVo edit(Long pId, PositionEditDto pPositionEditDto) {

        GenPositionEntity positionEntity = mPositionService.edit(pId, pPositionEditDto);
        PositionVo rtn = DtoEntityUtil.trans(positionEntity, PositionVo.class);
        return rtn;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public PositionVo delete(Long pId) {
        GenPositionEntity positionEntity = mPositionService.delete(pId);
        PositionVo rtn = DtoEntityUtil.trans(positionEntity, PositionVo.class);
        return rtn;
    }

    @Override
    public PositionVo info(Long pId) {
        GenPositionEntity positionEntity = mPositionService.check(pId);
        List<PositionTagMapperOut> positionTagMapperOutList = mPositionService.listByPositionId(pId);

        PositionVo rtn = DtoEntityUtil.trans(positionEntity, PositionVo.class);
        List<PositionTagMinVo> positionTagMinVos = DtoEntityUtil.trans(positionTagMapperOutList, PositionTagMinVo.class);
        rtn.setTags(positionTagMinVos);
        return rtn;
    }

    @Override
    public List<PositionVo> list() {
        List<GenPositionEntity> positions = mPositionService.list();
        List<PositionVo> rtn = DtoEntityUtil.trans(positions, PositionVo.class);
        return rtn;
    }
}
